package me.lubinn.Incantatio;

//import com.nijiko.permissions.PermissionHandler;
//import com.platymuus.bukkit.permissions.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.event.Event;
import org.bukkit.plugin.*;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.Configuration;

// Referenced classes of package me.orion.Incantatio:
//            IncantatioPlayerListener, IncantatioBlockListener, IncantatioEntityListener

public class Incantatio extends JavaPlugin
{

    public static boolean debug = false;
    public static File IncantatioLog = new File("Incantatio.log");
    public static File configFile;
    public static Properties prop = new Properties();
    public static Configuration config = null;
    public static Configuration oldconfig;
 //   public static PermissionHandler Permissions;
    public static Logger log = Logger.getLogger("Minecraft");
    private final IncantatioPlayerListener playerListener = new IncantatioPlayerListener(this);
    private final IncantatioBlockListener blockListener = new IncantatioBlockListener(this);
    private final IncantatioEntityListener entityListener = new IncantatioEntityListener(this);

    public Incantatio()
    {
    }

    public void onEnable()
    {
        if(debug)
        {
            log.info("[Incantatio-Debug] Loading configurations...");
        }
        ConfigLoad();
        log.info((new StringBuilder("Incantatio v")).append(getDescription().getVersion()).append(" successfully loaded.").toString());
        if(debug)
        {
            log.info("[Incantatio-Debug] Getting PluginManager");
        }
        PluginManager pm = getServer().getPluginManager();
        if(debug)
        {
            log.info("[Incantatio-Debug] Registering PLAYER_CHAT");
        }
        pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_CHAT, playerListener, org.bukkit.event.Event.Priority.Normal, this);
        if(debug)
        {
            log.info("[Incantatio-Debug] Registering PLAYER_MOVE");
        }
        pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_MOVE, playerListener, org.bukkit.event.Event.Priority.Normal, this);
        if(debug)
        {
            log.info("[Incantatio-Debug] Registering BLOCK_BREAK");
        }
        pm.registerEvent(org.bukkit.event.Event.Type.BLOCK_BREAK, blockListener, org.bukkit.event.Event.Priority.Normal, this);
        if(debug)
        {
            log.info("[Incantatio-Debug] Registering ENTITY_DAMAGE");
        }
        pm.registerEvent(org.bukkit.event.Event.Type.ENTITY_DAMAGE, entityListener, org.bukkit.event.Event.Priority.Normal, this);
        if(debug)
        {
            log.info("[Incantatio-Debug] Registering PLAYER_KICK");
        }
        pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_KICK, playerListener, org.bukkit.event.Event.Priority.Normal, this);
        if(debug)
        {
            log.info("[Incantatio-Debug] Checking if the log exists");
        }
        if(!IncantatioLog.exists())
        {
            try
            {
                if(debug)
                {
                    log.info("[Incantatio-Debug] Creating a new log");
                }
                IncantatioLog.createNewFile();
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
        if(debug)
        {
            log.info("[Incantatio-Debug] Attempting to get the Permissions plugin");
        }
        Plugin test = getServer().getPluginManager().getPlugin("Permissions");
        if(debug)
        {
            log.info("[Incantatio-Debug] Checking the existence of the Permissions plugin");
        }
       
        //DISABLING PERMISSIONS
        
        
        //if(Permissions == null)
     //   {
      //      if(test != null)
     //       {
     //           if(debug)
       //         {
       //             log.info("[Incantatio-Debug] Loading up Permissions");
         //       }
        //        Permissions = ((Permissions)test).getHandler();
        //        log.info("[Incantatio] Permissions plugin found, using that hierarchy.");
         //   } else
         //   {
                log.info("[Incantatio] Permissions plugin not found, everyone can cast spells.");
         //   }
       // }
    }

    private void ConfigLoad()
    {
        if(debug)
        {
            log.info("[Incantatio-Debug] Checking if Incantatio.config exists");
        }
        if(!configFile.exists())
        {
            log.info("[Incantatio] Configuration file does not exist! Creating one now...");
            ConfigCreate(false);
        } else
        {
            if(debug)
            {
                log.info("[Incantatio-Debug] Loading the old configuration file");
            }
            oldconfig.load();
            if(debug)
            {
                log.info("[Incantatio-Debug] Checking the old configuration file's version");
            }
            if(!oldconfig.getProperty("version").equals(getDescription().getVersion()))
            {
                log.info("[Incantatio] Configuration file out of date! Updating...");
                ConfigCreate(true);
            } else
            {
                if(debug)
                {
                    log.info("[Incantatio-Debug] Loading the old .config into the current one.");
                }
                config = oldconfig;
            }
        }
    }

    private void ConfigCreate(boolean update)
    {
        if(!update)
        {
            try
            {
                if(debug)
                {
                    log.info("[Incantatio-Debug] Creating a new Incantatio.config file");
                }
                configFile.createNewFile();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        config = new Configuration(configFile);
        if(debug)
        {
            log.info("[Incantatio-Debug] Storing all the properties...");
        }
        config.setProperty("version", getDescription().getVersion());
        ConfigPropertyStore("instant", "spells", true, update);
        ConfigPropertyStore("all", "spells_require_book", false, update);
        String spell = "salio";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        spell = "celeritas";
        ConfigPropertyStore(spell, "enabled", true, update);
        spell = "respiro";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        spell = "mutatio";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        spell = "vello";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "affects_players", true, update);
        ConfigPropertyStore(spell, "affects_self", true, update);
        spell = "urgeo";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "affects_players", true, update);
        ConfigPropertyStore(spell, "affects_self", true, update);
        spell = "lux";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "replaces_block", false, update);
        spell = "ignis_perpetua";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "replaces_block", false, update);
        spell = "extinguere";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        spell = "remedium";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "book_augment", true, update);
        spell = "accendit";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        spell = "praemium";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "book_augment", true, update);
        ConfigPropertyStore(spell, "replaces_block", false, update);
        spell = "ignis_spiritus";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        spell = "fulmen";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "book_augment", true, update);
        spell = "frigidus";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "cools_lava", true, update);
        ConfigPropertyStore(spell, "creates_snow", true, update);
        ConfigPropertyStore(spell, "freezes_water", true, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "concalesco";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "thaws_ice", true, update);
        ConfigPropertyStore(spell, "removes_snow", true, update);
        ConfigPropertyStore(spell, "melts_obsidian", false, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "sereno";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", true, update);
        spell = "pluvia";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", true, update);
        spell = "tempestas";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", true, update);
        spell = "noctem";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", true, update);
        spell = "lucem";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", true, update);
        spell = "implete_aquas";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "itero";
        ConfigPropertyStore(spell, "enabled", true, update);
        spell = "tueri";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        spell = "domus";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", true, update);
        spell = "evanescere";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "vitrea", true, update);
        ConfigPropertyStore(spell, "glacies", true, update);
        ConfigPropertyStore(spell, "silice", true, update);
        ConfigPropertyStore(spell, "aquae", true, update);
        ConfigPropertyStore(spell, "folia", true, update);
        ConfigPropertyStore(spell, "ignis", true, update);
        ConfigPropertyStore(spell, "aranea", true, update);
        ConfigPropertyStore(spell, "aeris", false, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "uanescere";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "vitrea", true, update);
        ConfigPropertyStore(spell, "glacies", true, update);
        ConfigPropertyStore(spell, "silice", true, update);
        ConfigPropertyStore(spell, "aquae", true, update);
        ConfigPropertyStore(spell, "folia", true, update);
        ConfigPropertyStore(spell, "ignis", true, update);
        ConfigPropertyStore(spell, "aranea", true, update);
        ConfigPropertyStore(spell, "aeris", false, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "bulla";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "vitrea", true, update);
        ConfigPropertyStore(spell, "glacies", true, update);
        ConfigPropertyStore(spell, "silice", true, update);
        ConfigPropertyStore(spell, "aquae", true, update);
        ConfigPropertyStore(spell, "folia", true, update);
        ConfigPropertyStore(spell, "ignis", true, update);
        ConfigPropertyStore(spell, "aranea", true, update);
        ConfigPropertyStore(spell, "aeris", false, update);
        ConfigPropertyStore(spell, "can_augere_vi", true, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "sphaera";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "vitrea", true, update);
        ConfigPropertyStore(spell, "glacies", true, update);
        ConfigPropertyStore(spell, "silice", true, update);
        ConfigPropertyStore(spell, "aquae", true, update);
        ConfigPropertyStore(spell, "folia", true, update);
        ConfigPropertyStore(spell, "ignis", true, update);
        ConfigPropertyStore(spell, "aranea", true, update);
        ConfigPropertyStore(spell, "aeris", true, update);
        ConfigPropertyStore(spell, "can_augere_vi", true, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "tholus";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "vitrea", true, update);
        ConfigPropertyStore(spell, "glacies", true, update);
        ConfigPropertyStore(spell, "silice", true, update);
        ConfigPropertyStore(spell, "aquae", true, update);
        ConfigPropertyStore(spell, "folia", true, update);
        ConfigPropertyStore(spell, "ignis", true, update);
        ConfigPropertyStore(spell, "aranea", true, update);
        ConfigPropertyStore(spell, "aeris", true, update);
        ConfigPropertyStore(spell, "can_augere_vi", true, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "libyes";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "vitrea", true, update);
        ConfigPropertyStore(spell, "glacies", true, update);
        ConfigPropertyStore(spell, "silice", true, update);
        ConfigPropertyStore(spell, "aquae", true, update);
        ConfigPropertyStore(spell, "folia", true, update);
        ConfigPropertyStore(spell, "ignis", true, update);
        ConfigPropertyStore(spell, "aranea", true, update);
        ConfigPropertyStore(spell, "aeris", false, update);
        spell = "annulus";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "vitrea", true, update);
        ConfigPropertyStore(spell, "glacies", true, update);
        ConfigPropertyStore(spell, "silice", true, update);
        ConfigPropertyStore(spell, "aquae", true, update);
        ConfigPropertyStore(spell, "folia", true, update);
        ConfigPropertyStore(spell, "ignis", true, update);
        ConfigPropertyStore(spell, "aranea", true, update);
        ConfigPropertyStore(spell, "aeris", false, update);
        ConfigPropertyStore(spell, "can_augere_vi", true, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        spell = "murus";
        ConfigPropertyStore(spell, "enabled", true, update);
        ConfigPropertyStore(spell, "consumes_book", false, update);
        ConfigPropertyStore(spell, "vitrea", true, update);
        ConfigPropertyStore(spell, "glacies", true, update);
        ConfigPropertyStore(spell, "silice", true, update);
        ConfigPropertyStore(spell, "aquae", true, update);
        ConfigPropertyStore(spell, "folia", true, update);
        ConfigPropertyStore(spell, "ignis", true, update);
        ConfigPropertyStore(spell, "aranea", true, update);
        ConfigPropertyStore(spell, "aeris", false, update);
        ConfigPropertyStore(spell, "can_augere_vi", true, update);
        ConfigPropertyStore(spell, "magna_parva", true, update);
        
        // Config for using ultima, supera, infera
        spell = "world_maiming";
        ConfigPropertyStore(spell, "enabled", true, update);
        
        
        //Fireball
     //   spell = "globus_ignis";
     //   ConfigPropertyStore(spell, "enabled", true, update);
     //   ConfigPropertyStore(spell, "consumes_book", false, update);
        
        if(debug)
        {
            log.info("[Incantatio-Debug] Saving the configuration file");
        }
        config.save();
    }

    private void ConfigPropertyStore(String spell, String property, boolean defaultvalue, boolean update)
    {
        String value = (new StringBuilder(String.valueOf(spell))).append("_").append(property).toString();
        if(update && oldconfig.getKeys().contains(value))
        {
            config.setProperty(value, oldconfig.getProperty(value));
        } else
        {
            config.setProperty(value, Boolean.valueOf(defaultvalue));
        }
    }

    public void onDisable()
    {
        log.info((new StringBuilder("Incantatio v")).append(getDescription().getVersion()).append(" successfully unloaded.").toString());
    }

    static 
    {
        configFile = new File("Incantatio.config");
        oldconfig = new Configuration(configFile);
    }
}
